<?php

/**
 * Client form base class.
 *
 * @method Client getObject() Returns the current form's model object
 *
 * @package    SdCommerce
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseClientForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                               => new sfWidgetFormInputHidden(),
      'code'                             => new sfWidgetFormInputText(),
      'matricule_fiscale'                => new sfWidgetFormInputText(),
      'raison_sociale'                   => new sfWidgetFormInputText(),
      'telephone1'                       => new sfWidgetFormInputText(),
      'telephone2'                       => new sfWidgetFormInputText(),
      'fax'                              => new sfWidgetFormInputText(),
      'email'                            => new sfWidgetFormInputText(),
      'site_web'                         => new sfWidgetFormInputText(),
      'code_douane'                      => new sfWidgetFormInputText(),
      'numero_exoneration_tva'           => new sfWidgetFormInputText(),
      'date_naissance'                   => new sfWidgetFormDate(),
      'date_changement_categorie_remise' => new sfWidgetFormDate(),
      'image'                            => new sfWidgetFormInputText(),
      'mise_en_sommeil'                  => new sfWidgetFormInputCheckbox(),
      'bloquer'                          => new sfWidgetFormInputCheckbox(),
      'observation'                      => new sfWidgetFormTextarea(),
      'solvabilite'                      => new sfWidgetFormInputCheckbox(),
      'gsm'                              => new sfWidgetFormInputText(),
      'numero_agrement'                  => new sfWidgetFormInputText(),
      'nom'                              => new sfWidgetFormInputText(),
      'prenom'                           => new sfWidgetFormInputText(),
      'fidele'                           => new sfWidgetFormInputCheckbox(),
      'id_forme_juridique'               => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('FormeJuridique'), 'add_empty' => true)),
      'id_nationalite'                   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Nationalite'), 'add_empty' => true)),
      'civilite'                         => new sfWidgetFormChoice(array('choices' => array('M' => 'M', 'Mme' => 'Mme', 'Mlle' => 'Mlle'))),
      'credit_max'                       => new sfWidgetFormInputText(),
      'id_modalite_paiement'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('ModalitePaiement'), 'add_empty' => true)),
      'solde'                            => new sfWidgetFormInputText(),
      'created_at'                       => new sfWidgetFormDateTime(),
      'updated_at'                       => new sfWidgetFormDateTime(),
      'objectifs_list'                   => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Objectif')),
      'categories_remise_list'           => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CategorieRemise')),
      'tarifs_list'                      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Tarif')),
    ));

    $this->setValidators(array(
      'id'                               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'code'                             => new sfValidatorString(array('max_length' => 225)),
      'matricule_fiscale'                => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'raison_sociale'                   => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'telephone1'                       => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'telephone2'                       => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'fax'                              => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'email'                            => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'site_web'                         => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'code_douane'                      => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'numero_exoneration_tva'           => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'date_naissance'                   => new sfValidatorDate(array('required' => false)),
      'date_changement_categorie_remise' => new sfValidatorDate(array('required' => false)),
      'image'                            => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'mise_en_sommeil'                  => new sfValidatorBoolean(array('required' => false)),
      'bloquer'                          => new sfValidatorBoolean(array('required' => false)),
      'observation'                      => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'solvabilite'                      => new sfValidatorBoolean(array('required' => false)),
      'gsm'                              => new sfValidatorString(array('max_length' => 32, 'required' => false)),
      'numero_agrement'                  => new sfValidatorNumber(array('required' => false)),
      'nom'                              => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'prenom'                           => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'fidele'                           => new sfValidatorBoolean(array('required' => false)),
      'id_forme_juridique'               => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('FormeJuridique'), 'required' => false)),
      'id_nationalite'                   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Nationalite'), 'required' => false)),
      'civilite'                         => new sfValidatorChoice(array('choices' => array(0 => 'M', 1 => 'Mme', 2 => 'Mlle'), 'required' => false)),
      'credit_max'                       => new sfValidatorNumber(array('required' => false)),
      'id_modalite_paiement'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('ModalitePaiement'), 'required' => false)),
      'solde'                            => new sfValidatorNumber(array('required' => false)),
      'created_at'                       => new sfValidatorDateTime(),
      'updated_at'                       => new sfValidatorDateTime(),
      'objectifs_list'                   => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Objectif', 'required' => false)),
      'categories_remise_list'           => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CategorieRemise', 'required' => false)),
      'tarifs_list'                      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Tarif', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('client[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Client';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['objectifs_list']))
    {
      $this->setDefault('objectifs_list', $this->object->Objectifs->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['categories_remise_list']))
    {
      $this->setDefault('categories_remise_list', $this->object->CategoriesRemise->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tarifs_list']))
    {
      $this->setDefault('tarifs_list', $this->object->tarifs->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveObjectifsList($con);
    $this->saveCategoriesRemiseList($con);
    $this->savetarifsList($con);

    parent::doSave($con);
  }

  public function saveObjectifsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['objectifs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Objectifs->getPrimaryKeys();
    $values = $this->getValue('objectifs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Objectifs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Objectifs', array_values($link));
    }
  }

  public function saveCategoriesRemiseList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['categories_remise_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CategoriesRemise->getPrimaryKeys();
    $values = $this->getValue('categories_remise_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CategoriesRemise', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CategoriesRemise', array_values($link));
    }
  }

  public function savetarifsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tarifs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->tarifs->getPrimaryKeys();
    $values = $this->getValue('tarifs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('tarifs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('tarifs', array_values($link));
    }
  }

}
